/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import bean.Cliente;
import bean.Internacional;
import bean.Nacional;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import main.Main;
import views.*;

public class PanelCliente extends javax.swing.JPanel {
	
	public int choice;
	
	ClienteTableModel miclienteTM = new ClienteTableModel();

	private void Actualizar(){
            miclienteTM.clientes=Main.service.queryAllClientes();
            miclienteTM.fireTableChanged(null);
        }
	
    private void groupButton( ) {

    ButtonGroup bg1 = new ButtonGroup( );

    bg1.add(RBNacional);
    bg1.add(RBInter);   
    }
	
	class ClienteTableModel extends AbstractTableModel{
        
               
        String Titulos[]={"ID","RUC","R.SOCIAL","DIRECCION","REPRESENTANTE","L.CREDITO","DEPARTAMENTO","CIUDAD","DIVISA","PAIS"};
        ArrayList<Cliente>clientes = Main.service.queryAllClientes(); 
               
        @Override
        public int getRowCount() {
            return clientes.size();
        }

        @Override
        public int getColumnCount() {
            return 10;
        }

        @Override
        public Object getValueAt(int fil, int col) {
            String res=null;
            Cliente c = clientes.get(fil);
            
            switch (col){
                case 0 : res=""+c.getId();break;
                case 1 : res= c.getRUC();break;
                case 2 : res= c.getRazonSocial();break;
                case 3 : res= c.getDireccion();break;
                case 4 : res= c.getRepresentante();break;
                case 5 : res=""+c.getLineaCred(); break;
                case 6 : if (c instanceof Nacional) {
                         res=((Nacional)c).getDepartamento();
                         break;
                        }
                else {
                    res="       -";
                    break;
                }
                case 7 : if (c instanceof Nacional) {
                         res=((Nacional)c).getCiudad();
                         break;
                        }
                else {
                    res="    -";
                    break;
                }
                case 8 : if (c instanceof Internacional) {
                         res=""+((Internacional)c).getDivisa();
                         break;
                        }
                else {
                    res="     -";
                    break;
                }
                case 9 : if (c instanceof Internacional) {
                         res=((Internacional)c).getPais();
                         break;
                        }
                else {
                    res="     -";
                    break;
                }
            }
            return res;
        }
        
        @Override
        public String getColumnName(int col){
            return Titulos[col];
        }
    }

    public PanelCliente() {
        initComponents();
        TablaCliente.setModel(miclienteTM);
        groupButton();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        TablaCliente = new javax.swing.JTable();
        jLabel12 = new javax.swing.JLabel();
        AcceptButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        txtPais = new javax.swing.JTextField();
        RBInter = new javax.swing.JRadioButton();
        txtDiv = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        RBNacional = new javax.swing.JRadioButton();
        txtCiu = new javax.swing.JTextField();
        txtDep = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        txtLinea = new javax.swing.JTextField();
        txtRep = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        txtRuc = new javax.swing.JTextField();
        txtRazon = new javax.swing.JTextField();
        txtDir = new javax.swing.JTextField();
        Editar = new javax.swing.JButton();
        ButtonDelete = new javax.swing.JButton();

        TablaCliente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        TablaCliente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TablaClienteMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TablaCliente);

        jLabel12.setText("Clientes Tramarsa");

        AcceptButton.setText("Agregar");
        AcceptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcceptButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("Limpiar");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Tipo Cliente", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 0)));

        jLabel11.setText("Pais:");

        txtPais.setEditable(false);
        txtPais.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtPaisKeyReleased(evt);
            }
        });

        RBInter.setSelected(true);
        RBInter.setText("Internacional");
        RBInter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RBInterActionPerformed(evt);
            }
        });

        txtDiv.setEditable(false);
        txtDiv.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtDivKeyReleased(evt);
            }
        });

        jLabel10.setText("Divisa:");

        jLabel8.setText("Departamento:");

        RBNacional.setText("Nacional");
        RBNacional.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RBNacionalActionPerformed(evt);
            }
        });

        txtCiu.setEditable(false);
        txtCiu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCiuActionPerformed(evt);
            }
        });
        txtCiu.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCiuKeyReleased(evt);
            }
        });

        txtDep.setEditable(false);
        txtDep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDepActionPerformed(evt);
            }
        });
        txtDep.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtDepKeyReleased(evt);
            }
        });

        jLabel9.setText("Ciudad:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RBInter)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11))
                        .addGap(32, 32, 32)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtPais)
                            .addComponent(txtDiv, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 113, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtDep)
                            .addComponent(txtCiu, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(RBNacional))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(RBInter)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(txtDiv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(txtPais, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(45, 45, 45)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel8)
                                    .addComponent(txtDep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(RBNacional))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCiu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Datos Cliente"));

        txtLinea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtLineaActionPerformed(evt);
            }
        });
        txtLinea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtLineaKeyReleased(evt);
            }
        });

        txtRep.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtRepKeyReleased(evt);
            }
        });

        jLabel1.setText("Id:");

        jLabel3.setText("Razón Social:");

        jLabel2.setText("Ruc:");

        jLabel4.setText("Dirección:");

        jLabel6.setText("Representante:");

        jLabel7.setText("Linea de Crédito:");

        txtId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIdActionPerformed(evt);
            }
        });
        txtId.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtIdKeyReleased(evt);
            }
        });

        txtRuc.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtRucKeyReleased(evt);
            }
        });

        txtRazon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtRazonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(278, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtRep)
                    .addComponent(txtLinea, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3)
                        .addComponent(jLabel4))
                    .addGap(46, 46, 46)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtDir)
                        .addComponent(txtRazon)
                        .addComponent(txtRuc)
                        .addComponent(txtId, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE))
                    .addContainerGap(256, Short.MAX_VALUE)))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtRep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(txtLinea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 36, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(4, 4, 4)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(txtRuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(txtRazon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(txtDir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        Editar.setText("Editar");
        Editar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditarActionPerformed(evt);
            }
        });

        ButtonDelete.setText("Eliminar");
        ButtonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonDeleteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(AcceptButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(33, 33, 33)
                                .addComponent(Editar, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(28, 28, 28)
                                .addComponent(CancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(38, 38, 38)
                                .addComponent(ButtonDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AcceptButton)
                    .addComponent(CancelButton)
                    .addComponent(Editar)
                    .addComponent(ButtonDelete))
                .addGap(18, 18, 18)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(59, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
	
	private void txtLineaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtLineaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtLineaActionPerformed

    private void txtIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIdActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtIdActionPerformed

    private void RBInterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RBInterActionPerformed
        txtDep.setEditable(false);//txtDep.setVisible(false);
        txtDep.setText("");
        txtCiu.setText("");
        txtCiu.setEditable(false);//txtCiu.setVisible(false);asd
        txtDiv.setEditable(true);//txtDiv.setVisible(true);
        txtPais.setEditable(true);//txtPais.setVisible(true);
        choice = 1;
    }//GEN-LAST:event_RBInterActionPerformed

    private void RBNacionalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RBNacionalActionPerformed
        txtDep.setEditable(true);
        txtDiv.setText("");
        txtPais.setText("");
        //txtDep.setVisible(true);
        txtCiu.setEditable(true);//txtCiu.setVisible(true);
        txtDiv.setEditable(false);//txtDiv.setVisible(false);
        txtPais.setEditable(false);//txtPais.setVisible(false);
        choice = 0;
    }//GEN-LAST:event_RBNacionalActionPerformed

    private void txtCiuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCiuActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCiuActionPerformed

    private void txtDepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDepActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDepActionPerformed

    private void txtRazonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtRazonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtRazonActionPerformed

    private void AcceptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcceptButtonActionPerformed
        if(choice==0){//Nacional
            Nacional n = new Nacional(txtDep.getText(),txtCiu.getText());
            n.setId(Integer.parseInt(txtId.getText()));
            n.setRUC(txtRuc.getText());
            n.setRazonSocial(txtRazon.getText());
            n.setDireccion(txtDir.getText());
            n.setRepresentante(txtRep.getText());
            n.setLineaCred(Float.parseFloat(txtLinea.getText()));
            n.setDepartamento(txtDep.getText());
            n.setCiudad(txtCiu.getText());  
            n.setTipo("N");
            
            Main.service.agregaCliente(n);
             Actualizar();
        }
        else{//Internacional
            Internacional n = new Internacional(Float.parseFloat(txtDiv.getText()),txtPais.getText());
            n.setId(Integer.parseInt(txtId.getText()));
            n.setRUC(txtRuc.getText());
            n.setRazonSocial(txtRazon.getText());
            n.setDireccion(txtDir.getText());
            n.setRepresentante(txtRep.getText());
            n.setLineaCred(Float.parseFloat(txtLinea.getText()));
            n.setDivisa(Float.parseFloat(txtDiv.getText()));
            n.setPais(txtPais.getText());
            n.setTipo("I");
            Main.service.agregaCliente(n);
            Actualizar();
        }
     
 
    }//GEN-LAST:event_AcceptButtonActionPerformed

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        txtId.setText("");
        txtRuc.setText("");
        txtRazon.setText("");
        txtDir.setText("");
        txtRep.setText("");
        txtLinea.setText("");
    }//GEN-LAST:event_CancelButtonActionPerformed

    private void EditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditarActionPerformed
         if(choice==0){//Nacional
            Nacional n = new Nacional(txtDep.getText(),txtCiu.getText());
            n.setId(Integer.parseInt(txtId.getText()));
        n.setId(Integer.parseInt(txtId.getText()));
        n.setLineaCred(Float.parseFloat(txtLinea.getText()));
        n.setDireccion(txtDir.getText());
        n.setRUC(txtRuc.getText());
        n.setRazonSocial(txtRazon.getText());
        n.setRepresentante(txtRep.getText());
        Main.service.editaCliente(n);
         }
         else{//Internacional
            Internacional n = new Internacional(Float.parseFloat(txtDiv.getText()),txtPais.getText());
            n.setId(Integer.parseInt(txtId.getText()));
        n.setId(Integer.parseInt(txtId.getText()));
        n.setDireccion(txtDir.getText());
        n.setLineaCred(Float.parseFloat(txtLinea.getText()));
        n.setRUC(txtRuc.getText());
        n.setRazonSocial(txtRazon.getText());
        n.setRepresentante(txtRep.getText());

  Main.service.editaCliente(n);
         }
               Actualizar();
    }//GEN-LAST:event_EditarActionPerformed

    private void txtIdKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIdKeyReleased
      
                if( ! txtId.getText( ).matches( "^([0-9]*)$" )  ){
        JOptionPane.showMessageDialog(null,"Dato del Id no valido","Error!",JOptionPane.ERROR_MESSAGE);
 txtId.setText("");      
}                  
      else if(txtId.getText( ).matches( "[0-9]*" )){
          String str=txtId.getText();
        int tam=str.length();
        if (tam>8) {
            JOptionPane.showMessageDialog(null,"El ID permite máximo 8 caracteres","Error!",JOptionPane.ERROR_MESSAGE); 
        txtId.setText("");
        }   
}
               
    }//GEN-LAST:event_txtIdKeyReleased

    private void txtRucKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRucKeyReleased
        // TODO add your handling code here:
                  if( ! txtRuc.getText( ).matches( "[0-9a-zA-Z]*" )  ){
        JOptionPane.showMessageDialog(null,"Dato del RUC no valido","Error!",JOptionPane.ERROR_MESSAGE);
 txtRuc.setText("");      
}  

                  else if(txtRuc.getText( ).matches( "[0-9a-zA-Z]*" )){
          String str=txtRuc.getText();
        int tam=str.length();
        if (tam>8) {
            JOptionPane.showMessageDialog(null,"El RUC permite máximo 8 caracteres","Error!",JOptionPane.ERROR_MESSAGE); 
        txtRuc.setText("");
        }   
}
               
    
        
    }//GEN-LAST:event_txtRucKeyReleased

    private void txtLineaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtLineaKeyReleased
        // TODO add your handling code here:
                 
try
    { float f = Float.parseFloat( txtLinea.getText() );
      float p_dec=f-(int)f; // OBTENGO PARTE DECIMAL
                               f=f*100;
                               float part=f-(int)f;                         
                               int aux=(int)f;
                               p_dec=aux/100;                                            
                if (part>0 ) { //tienes mas de dos decimales {
    JOptionPane.showMessageDialog(null,"Solo se permite 2 decimales","Error!",JOptionPane.ERROR_MESSAGE); 
     txtLinea.setText("");
}
    }
catch( NumberFormatException e )
{    JOptionPane.showMessageDialog(null,"Linea de Credito no es correcta","Error!",JOptionPane.ERROR_MESSAGE); 
     txtLinea.setText("");
     
}
    }//GEN-LAST:event_txtLineaKeyReleased

    private void txtRepKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRepKeyReleased
        // TODO add your handling code here:
          if(!txtRep.getText().matches("[a-zA-Z ]*")){
            JOptionPane.showMessageDialog(null,"Campo Representante no valido","Error!",JOptionPane.ERROR_MESSAGE); 
        
        txtRep.setText("");
        } 
    }//GEN-LAST:event_txtRepKeyReleased

    private void txtDivKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDivKeyReleased
       try
    { float f = Float.parseFloat( txtDiv.getText() );
      float p_dec=f-(int)f; // OBTENGO PARTE DECIMAL
                               f=f*100;
                               float part=f-(int)f;                         
                               int aux=(int)f;
                               p_dec=aux/100;                                            
                if (part>0 ) { //tienes mas de dos decimales {
    JOptionPane.showMessageDialog(null,"Solo se permite 2 decimales","Error!",JOptionPane.ERROR_MESSAGE); 
     txtDiv.setText("");
}
    }
catch( NumberFormatException e )
{    JOptionPane.showMessageDialog(null,"Divisa no es correcta","Error!",JOptionPane.ERROR_MESSAGE); 
     txtDiv.setText("");
    
}
        
        
    }//GEN-LAST:event_txtDivKeyReleased

    private void txtPaisKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPaisKeyReleased
          if(!txtPais.getText().matches("[a-zA-Z ]*")){
            JOptionPane.showMessageDialog(null,"Pais no valido","Error!",JOptionPane.ERROR_MESSAGE); 
        
        txtPais.setText("");
        } 
    }//GEN-LAST:event_txtPaisKeyReleased

    private void txtDepKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDepKeyReleased
        // TODO add your handling code here:
         if(!txtDep.getText().matches("[a-zA-Z ]*")){
            JOptionPane.showMessageDialog(null,"Departamento no valido","Error!",JOptionPane.ERROR_MESSAGE); 
        
        txtDep.setText("");
        } 
    }//GEN-LAST:event_txtDepKeyReleased

    private void txtCiuKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCiuKeyReleased

         if(!txtCiu.getText().matches("[a-zA-Z ]*")){
            JOptionPane.showMessageDialog(null,"Ciudad no valido","Error!",JOptionPane.ERROR_MESSAGE); 
        
        txtCiu.setText("");
        } 
    }//GEN-LAST:event_txtCiuKeyReleased

    private void TablaClienteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablaClienteMouseClicked
        // TODO add your handling code here:
           int rowSel = TablaCliente.getSelectedRow();
        String idCliente=TablaCliente.getValueAt(rowSel,0).toString();
        String ruc=TablaCliente.getValueAt(rowSel,1).toString();
        String razon=TablaCliente.getValueAt(rowSel,2).toString();
        String dir=TablaCliente.getValueAt(rowSel,3).toString();
        String Rep=TablaCliente.getValueAt(rowSel,4).toString();
        String Lin=TablaCliente.getValueAt(rowSel,5).toString();
        String Dep= TablaCliente.getValueAt(rowSel,6).toString();
        String Ciu=TablaCliente.getValueAt(rowSel,7).toString();
        String Div=TablaCliente.getValueAt(rowSel,8).toString();
        String Pais=TablaCliente.getValueAt(rowSel,9).toString();
        
        Cliente cli=Main.service.queryByCliente(idCliente);
 
        txtId.setText(idCliente);
        txtRuc.setText(ruc);
        txtRazon.setText(razon);    
        txtDir.setText(dir);
        txtRep.setText(Rep);
        txtLinea.setText(Lin);
        txtDep.setText(Dep);
        txtCiu.setText(Ciu);
        txtDiv.setText(Div);
        txtPais.setText(Pais);
//       
//         if (txtDiv.isEditable())  {
//        RBInter.setSelected(true);
//        RBNacional.setSelected(false);
//          }
//           else if(txtDiv.getV())   {        
//                RBNacional.setSelected(true);   
//                RBInter.setSelected(false);
//           } 
        
    }//GEN-LAST:event_TablaClienteMouseClicked

    private void ButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonDeleteActionPerformed
        int rowSel=TablaCliente.getSelectedRow();
        int id=Integer.parseInt(TablaCliente.getValueAt(rowSel,0).toString());
        Main.service.eliminaCliente(id);
        Actualizar();
    }//GEN-LAST:event_ButtonDeleteActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AcceptButton;
    private javax.swing.JButton ButtonDelete;
    private javax.swing.JButton CancelButton;
    private javax.swing.JButton Editar;
    private javax.swing.JRadioButton RBInter;
    private javax.swing.JRadioButton RBNacional;
    private javax.swing.JTable TablaCliente;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtCiu;
    private javax.swing.JTextField txtDep;
    private javax.swing.JTextField txtDir;
    private javax.swing.JTextField txtDiv;
    private javax.swing.JTextField txtId;
    private javax.swing.JTextField txtLinea;
    private javax.swing.JTextField txtPais;
    private javax.swing.JTextField txtRazon;
    private javax.swing.JTextField txtRep;
    private javax.swing.JTextField txtRuc;
    // End of variables declaration//GEN-END:variables
}
